﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMSGSC.DAL;
using PMSGSC.BLL;
using System.IO;
using DevExpress.XtraBars.Ribbon;
using System.Threading;
using PMSGSC.Common;
namespace PMSGSC.App
{
    public partial class frmSelectShip : Form
    {
        List<Ship> shipList;
        public frmSelectShip()
        {
            InitializeComponent();
            shipList = Ship_BLL.GetAll();
            GalleryItemGroup group1 = galleryControl1.Gallery.Groups[0];
            string Address = Application.StartupPath + "\\Attachment\\";
            for (int i = 0; i < shipList.Cast<Ship>().ToList().Count; i++)
            {
                if (shipList[i].Image != null)
                {
                    Directory.CreateDirectory(Address);
                    string fileName = shipList[i].NameShip + shipList[i].Extension;
                    Address += fileName;
                    ByteArrayToFile(Address, shipList[i].Image.ToArray());
                    Image img = Image.FromFile(Address);
                    group1.Items.Add(new GalleryItem(img, shipList[i].NameShip, shipList[i].Details));
                }
                else
                {
                    group1.Items.Add(new GalleryItem(null, shipList[i].NameShip, shipList[i].Details));
                }
            }
            LoadSkin();
        }
        private static void LoadSkin()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Blue");
            }
        }

        public static void ThreadProc()
        {
            Application.Run(new frmMain());
        }
        public bool ByteArrayToFile(string fileName, byte[] byteArray)
        {
            try
            {
                // Open file for reading
                System.IO.FileStream fileStream = new System.IO.FileStream
                    (fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                // Writes a block of bytes
                fileStream.Write(byteArray, 0, byteArray.Length);

                // Close file stream
                fileStream.Close();

                return true;
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
            }
            return false;
        }

        private void galleryControl1_Gallery_ItemClick(object sender, GalleryItemClickEventArgs e)
        {
        //    objMain.lbShipName.Text = e.Item.Caption;
            for (int i = 0; i < shipList.Cast<Ship>().ToList().Count; i++)
            {
                if (shipList[i].NameShip == e.Item.Caption)
                {
                    frmMain.ship = shipList[i];
                    break;
                }
            }
            if (!frmMain.flag)
            {
                Thread thread = new Thread(new ThreadStart(ThreadProc));
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();              
            }
            this.Close();
        }
    }
}
